#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#Kiwi Bot
#Copyright (C) 2009 Mathieu "MathX" Xhonneux
#Modified by @nge1nCorsic4 in 2010
#Modified by @nge1nCorsic4 in 2011
#Modified by @nge1nCorsic4 in 2012
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import kiwi, kiwi.events, kiwi.game, kiwi.clients, kiwi.rcon, kiwi.config, kiwi.lang
import sys, time
class Interaction:
	def __init__(self,main):
		self.lang = kiwi.lang.Lang(self,'base')
		self.c = kiwi.config.configuration()
		self.main = main
		self.log = self.main.log
		self.plugins = {}
		self.loadHandlers()
		self.rcon = kiwi.rcon.rcon(self, self.c.server.ip,int(self.c.server.port),self.c.server.rcon)
		self.game = kiwi.game.Game(self)
		self.clients = kiwi.clients.Clients(self)
	def loadHandlers(self):
		self.handlers = {}
		i = 0
		for evt in kiwi.events.events.allEvents:
			self.handlers[i] = []
			i += 1
	def registerHandler(self, event, attr):
		if event in self.handlers:
			self.handlers[event].append(attr)
	def handleEvent(self, event):
		if event.type == kiwi.events.EVT_START:
			self.loadAllPlugins(True)
		if event.type == kiwi.events.EVT_STOP:
			self.stopAllPlugins()
			sys.exit(0)
		if event.type == kiwi.events.EVT_GAME_START:
			self.game.gameStarted = True
		if self.game.gameStarted:
			for i in self.handlers:
				if event.type == i:
					for func in self.handlers[i]:
						func(event)
	def loadPlugin(self, name):
		try:
			path = 'kiwi.plugins.%s'%(name)
			mod = __import__(path)
			path = path.split('.')
			for comp in path[1:]:
             			mod = getattr(mod, comp)
		except ImportError:
			mod = None
			self.log.warn(self.lang.lang('', '10008')%(name))
		if mod:
			mod = getattr(mod,name)
			self.plugins[name] = Plugin(name,mod(self))
			self.log.info(self.lang.lang('', '10009')%(name))
	def loadAllPlugins(self, startAll = False):
		for plugin in self.c.plugins._list:
			if getattr(self.c.plugins,plugin, 'no').lower() == 'on':
				self.loadPlugin(plugin)
		if startAll:
			self.plugins['admin'].start()
			self.startAllPlugins()
	def startAllPlugins(self):
		for plugin in self.plugins:
			if not 'admin' in plugin:
				self.plugins[plugin].start()
	def startPlugin(self, namePlugin):
		if namePlugin in self.plugins:
			self.plugins[namePlugin].start()
			return True
		return False
	def stopPlugin(self, namePlugin):
		if namePlugin in self.plugins:
			self.plugins[namePlugin].stop()
		del self.plugins[namePlugin]
	def stopAllPlugins(self):
		for plugin in self.plugins:
			self.plugins[plugin].stop()
		self.plugins = {}
	def getPlugin(self,name):
		if name in self.plugins:
			return self.plugins[name]
		return None
class Plugin:
	def __init__(self,name, mod):
		self.name, self.mod, self.status = name, mod, False
	def start(self):
		self.mod.start()
	def stop(self):
		self.mod.stop()